//
//  ScrView.swift
//  MyFood
//
//  Created by dllo on 16/2/16.
//  Copyright © 2016年 XRG. All rights reserved.
//

import UIKit
protocol ScrViewDelegate {
    func click(num : Int)
}

class ScrView: BaseView, UICollectionViewDelegate, UICollectionViewDataSource {

    var collectionView : UICollectionView!
    var delegate : ScrViewDelegate!
    
    var bannerArr: Array<F_LunBo_Model>?{

        didSet{
            self.collectionView.reloadData()
        }
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        createCollectionView()
    }

    func createCollectionView(){

        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSizeMake(self.frame.width, self.frame.height)
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        self.collectionView = UICollectionView(frame: CGRectMake(0, 0, BASE_WIDTH, self.frame.height), collectionViewLayout: flowLayout)
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        self.addSubview(self.collectionView)

        self.collectionView.registerNib(UINib(nibName: "ScrView_CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")

        collectionView.pagingEnabled = true
        collectionView.bounces = false
        self.collectionView.delegate = self
        self.collectionView.dataSource = self

    }


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if self.bannerArr != nil {
            return (self.bannerArr?.count)!
        }
        return 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell : ScrView_CollectionViewCell? = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? ScrView_CollectionViewCell

        let model = self.bannerArr![indexPath.row]

        cell?.image.sd_setImageWithURL(NSURL(string: model.banner_picture), placeholderImage: UIImage(named: "空白1"))
        
        return cell!
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        self.delegate?.click(indexPath.row)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
